package com.superatom.designpattern.singleton.lazysingleton;

import java.util.concurrent.CountDownLatch;

public class LazySingletonMain {

    public static void main(String[] args) {
        int count = 50;
        // 保证所有线程同时执行
        CountDownLatch latch = new CountDownLatch(count);
        CountDownLatch over = new CountDownLatch(count);

        long start = System.currentTimeMillis();

        for (int i = 0; i < count; i++){
            new Thread(){
                @Override
                public void run() {
                    try {
                        latch.countDown();
                        latch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LazyStaticSingleton lazySingleton = LazyStaticSingleton.getInstance();
                    // LazySingleton lazySingleton = LazySingleton.getInstance();
                    // LazySyncSingleton lazySingleton = LazySyncSingleton.getInstance();
                    System.out.println(System.currentTimeMillis() + ":" + lazySingleton);
                    over.countDown();
                }
            }.start();
        }

        try {
            over.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("总好时：" + (System.currentTimeMillis() - start) + "ms");
    }
}
